package com.cn.wanxi.dao.impl;

import com.cn.wanxi.dao.ProductFindAllDao;
import com.cn.wanxi.model.DesignerModel;
import com.cn.wanxi.model.ProductModel;
import com.cn.wanxi.util.JDBC;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class ProductFindAllImpl implements ProductFindAllDao {
    @Override
    public List<ProductModel> findAll(ProductModel model) {
        return null;
    }

    @Override
    public int add(ProductModel model) {
        return 0;
    }

    @Override
    public int update(ProductModel model) {
        return 0;
    }

    @Override
    public ProductModel findById(ProductModel model) {
        return null;
    }

    @Override
    public int delete(ProductModel model) {
        return 0;
    }

    @Override
    public int getCount(ProductModel model) {
        return 0;
    }

    @Override
    public List<ProductModel> getProductFindAll() {
        String sql = "select * from product where enable = 1 order by id asc limit 8";
        ResultSet resultSet = JDBC.query(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel productModel = new ProductModel();
                productModel.setId(resultSet.getInt("id"));
                productModel.setName(resultSet.getString("name"));
                productModel.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                productModel.setMarketPrice(resultSet.getBigDecimal("price"));
                productModel.setImgHref(resultSet.getString("img_href"));
                productModel.setIsShow(resultSet.getInt("is_show"));
                list.add(productModel);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }

    @Override
    public List<ProductModel> getProductInformationById(ProductModel productModel) {
        String sql = "(SELECT * FROM product where id=(select max(id) from product where id<"+productModel.getId()+"))";
        sql += "union all";
        sql += "(SELECT * FROM product where id="+productModel.getId()+")";
        sql += "union all";
        sql += "(SELECT * FROM product where id=(select min(id) from product where id>"+productModel.getId()+"))";
        ResultSet resultSet = JDBC.query(sql);
        List<ProductModel> list = new ArrayList<>();
        try {
            while (resultSet.next()) {
                ProductModel model = new ProductModel();
                model.setId(resultSet.getInt("id"));
                model.setName(resultSet.getString("name"));
                model.setNormalPrice(resultSet.getBigDecimal("normal_price"));
                model.setMarketPrice(resultSet.getBigDecimal("price"));
                model.setContent(resultSet.getString("content"));
                model.setIsShow(resultSet.getInt("is_show"));
                model.setImgHref(resultSet.getString("img_href"));
                model.setCreateTime(resultSet.getString("create_time"));
                model.setEnable(resultSet.getInt("enable"));
                list.add(model);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return list;
    }
}
